package com.archstoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.FollowListEntity;
import com.base.activity.BaseActivity;
import com.base.utils.ImageUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的关注列表适配器
 */
public class MyFollowAdapter extends BaseAdapter {

    private Context mContext;
    private BaseActivity.Click mClick;

    private List<FollowListEntity.DataBean> mData;

    public MyFollowAdapter(Context context, BaseActivity.Click click) {
        mContext = context;
        mClick = click;
    }

    public void setData(List<FollowListEntity.DataBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addData(List<FollowListEntity.DataBean> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void removeData(int index) {
        mData.remove(index);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public FollowListEntity.DataBean getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_my_follow, null);
            holder.headIcon = (CircleImageView) view.findViewById(R.id.item_head_icon);
            holder.nameTv = (TextView) view.findViewById(R.id.item_name_tv);
            holder.cancelBtn = (TextView) view.findViewById(R.id.item_cancel_btn);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.nameTv.setText(mData.get(i).getUserName());
        ImageUtil.setCropBitmap(mContext, holder.headIcon,
                mData.get(i).getUserImg(), R.mipmap.user_header_icon);

        holder.cancelBtn.setTag(i);
        holder.cancelBtn.setOnClickListener(mClick);

        return view;
    }

    class Holder {
        CircleImageView headIcon;
        TextView nameTv, cancelBtn;
    }
}